/*
 * AGENT SHERPA
 *  
 * Assignement 2 of the Agent Oriented Software Engineering Course
 * M.Sc. in Advanced Software Engineering (Part-time)
 * University College Dublin www.ucd.ie
 *
 * Copyright (c) 2007 Chris Walsh, Aidan Morrisy and Iain Hull.
 * All rights reserved.
 */package org.agentsherpa;

import org.agentsherpa.model.GlobalMap;
import org.agentsherpa.model.Point;

import com.agentfactory.platform.interfaces.Perceptor;

/**
 * The MapPerceptor provides agent beliefs, for there curent view of the world.
 * It interfaces with the LocationModule to provide this data.
 */
public class MapPerceptor extends Perceptor {
    /**
     * percieve the current state of the known map.
     * 
     * This is only an example of possible beliefs.
     */
    public void perceive() {
		System.out.println("MapPerceptor: start");
        try {
            SimulatorService simulator = getSimulator();
            
            // TODO move to module
            // currently returns full map
            
            GlobalMap map = simulator.getMap();
			for(int x = 0; x < map.getWidth(); ++x) {
                for(int y = 0; y < map.getHeight(); ++y) {
                    Point p = new Point(x, y);
                    if (map.isRoad(p)) {
                        System.out.print("*");
                        adoptBelief("BELIEF(road" + p + ")");                  
                    }
                    else {
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
        }
        catch(PerceptorException e) {
            e.printStackTrace();
        }
		System.out.println("MapPerceptor: end");
    }
    
    private SimulatorService getSimulator() throws PerceptorException {
        SimulatorService simulator = (SimulatorService) getService("org.agentsherpa.SimulatorService");
        if (simulator == null) {
            throw new PerceptorException("Cannot access SimulatorService");
        }
        return simulator;
    }
}
